<%@ page import="com.library.entity.Payrec" %>
<%@ page import="java.util.List" %>
<%@ page import="java.text.DecimalFormat" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="com.library.entity.Admin" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>图书管理系统 - 学生罚款记录</title>
    <style>
        /* 页面整体样式 */
        body {
            margin: 0;
            padding: 0;
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
        }

        /* 顶部导航栏样式 */
        .header {
            background-color: #f8f9fa;
            padding: 10px 20px;
            border-bottom: 1px solid #e0e0e0;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .header h1 {
            margin: 0;
            font-size: 20px;
            color: #333;
        }

        .user-info {
            display: flex;
            align-items: center;
        }

        .logout-btn {
            margin-left: 15px;
            padding: 5px 10px;
            background-color: #f8f9fa;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            color: #333;
        }

        /* 左侧导航栏样式 */
        .nav-container {
            width: 200px;
            background-color: #f8f9fa;
            float: left;
            min-height: calc(100vh - 50px);
            border-right: 1px solid #e0e0e0;
        }

        .admin-label {
            padding: 15px;
            background-color: #e9ecef;
            font-weight: bold;
            border-bottom: 1px solid #ddd;
        }

        .nav-container ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .nav-container li {
            padding: 15px;
            cursor: pointer;
            border-bottom: 1px solid #e0e0e0;
        }

        .nav-container li:hover {
            background-color: #e9ecef;
        }

        .nav-container li a {
            text-decoration: none;
            color: #333;
            display: block;
        }

        .nav-container li.active {
            background-color: #007bff;
        }

        .nav-container li.active a {
            color: white;
        }

        /* 中间内容区域样式 */
        .content-container {
            margin-left: 200px;
            padding: 20px;
        }

        /* 搜索框样式 */
        .search-container {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }

        .search-form {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
            align-items: center;
        }

        .search-group {
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .search-group label {
            font-weight: bold;
            min-width: 60px;
        }

        .search-group input {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        .search-group input[type="text"] {
            width: 150px;
        }

        .search-group input[type="date"] {
            width: 130px;
        }

        /* 隐藏日期输入框的占位符 */
        input[type="date"]::-webkit-datetime-edit-text,
        input[type="date"]::-webkit-datetime-edit-month-field,
        input[type="date"]::-webkit-datetime-edit-day-field,
        input[type="date"]::-webkit-datetime-edit-year-field {
            color: transparent;
        }

        input[type="date"]:valid::-webkit-datetime-edit-text,
        input[type="date"]:valid::-webkit-datetime-edit-month-field,
        input[type="date"]:valid::-webkit-datetime-edit-day-field,
        input[type="date"]:valid::-webkit-datetime-edit-year-field {
            color: #333;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            font-size: 14px;
        }

        .btn-primary {
            background-color: #007bff;
            color: white;
        }

        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }

        .btn:hover {
            opacity: 0.9;
        }

        /* 错误信息样式 */
        .error-message {
            background-color: #f8d7da;
            color: #721c24;
            padding: 10px;
            border-radius: 4px;
            margin-bottom: 15px;
            border: 1px solid #f5c6cb;
        }

        /* 表格样式 */
        .fine-table {
            width: 100%;
            border-collapse: collapse;
            background-color: white;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        .fine-table th, .fine-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .fine-table th {
            background-color: #f8f9fa;
            font-weight: bold;
        }

        .fine-table tr:hover {
            background-color: #f5f5f5;
        }

        /* 分页样式 */
        .pagination {
            margin-top: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .pagination a {
            padding: 8px 12px;
            margin: 0 5px;
            border: 1px solid #ddd;
            text-decoration: none;
            color: #007bff;
            border-radius: 4px;
        }

        .pagination a:hover {
            background-color: #f5f5f5;
        }

        .pagination a.active {
            background-color: #007bff;
            color: white;
            border-color: #007bff;
        }

        .pagination .disabled {
            color: #999;
            cursor: not-allowed;
        }

        /* 统计信息样式 */
        .statistics {
            display: flex;
            margin-bottom: 20px;
        }

        .stat-card {
            flex: 1;
            background-color: white;
            padding: 15px;
            margin-right: 15px;
            border-radius: 5px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }

        .stat-card:last-child {
            margin-right: 0;
        }

        .stat-card h3 {
            margin-top: 0;
            color: #333;
            font-size: 16px;
        }

        .stat-value {
            font-size: 24px;
            font-weight: bold;
            color: #007bff;
        }

        .empty-message {
            text-align: center;
            padding: 40px;
            color: #666;
            font-size: 16px;
            background-color: white;
            border-radius: 5px;
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<div class="header">
    <h1>图书管理系统</h1>
    <div class="user-info">
        欢迎您，<%=((Admin) request.getSession().getAttribute("admin")).getUsername()%>
        <a href="../common?method=logout" class="logout-btn">退出登录</a>
    </div>
</div>

<!-- 左侧导航栏 -->
<div class="nav-container">
    <div class="admin-label">管理端</div>
    <ul>
        <li><a href="../admin?method=adminIndex">首页</a></li>
        <li><a href="../admin/book?method=bookManage">书籍</a></li>
        <li><a href="../admin/bookcopy?method=bookCopyManage">书籍副本</a></li>
        <li class="active"><a href="../admin/payrec?method=fineRecord">学生罚款记录</a></li>
    </ul>
</div>

<!-- 中间内容区域 -->
<div class="content-container">
    <h2>学生罚款记录</h2>

    <!-- 统计信息 -->
    <div class="statistics">
        <%
            Integer totalCount = (Integer) request.getAttribute("totalCount");
            BigDecimal totalAmount = (BigDecimal) request.getAttribute("totalAmount");
            DecimalFormat df = new DecimalFormat("#,##0.00");
        %>
        <div class="stat-card">
            <h3>罚款记录总数</h3>
            <div class="stat-value"><%=totalCount != null ? totalCount : 0%></div>
        </div>
        <div class="stat-card">
            <h3>当前页面总金额</h3>
            <div class="stat-value">¥<%=totalAmount != null ? df.format(totalAmount) : "0.00"%></div>
        </div>
    </div>

    <!-- 错误信息显示 -->
    <%
        String error = (String) request.getAttribute("error");
        if (error != null) {
    %>
    <div class="error-message">
        <%=error%>
    </div>
    <%
        }
    %>

    <!-- 搜索框 -->
    <div class="search-container">
        <form action="<%=request.getContextPath()%>/admin/payrec" method="get" class="search-form">
            <input type="hidden" name="method" value="fineRecord">

            <div class="search-group">
                <label for="keyword">学号:</label>
                <input type="text" id="keyword" name="keyword" placeholder="输入学号"
                       value="<%=request.getAttribute("keyword") != null ? request.getAttribute("keyword") : ""%>">
            </div>

            <div class="search-group">
                <label for="startDate">开始日期:</label>
                <input type="date" id="startDate" name="startDate"
                       value="<%=request.getAttribute("startDate") != null ? request.getAttribute("startDate") : ""%>">
            </div>

            <div class="search-group">
                <label for="endDate">结束日期:</label>
                <input type="date" id="endDate" name="endDate"
                       value="<%=request.getAttribute("endDate") != null ? request.getAttribute("endDate") : ""%>">
            </div>

            <button type="submit" class="btn btn-primary">搜索</button>
            <a href="<%=request.getContextPath()%>/admin/payrec?method=fineRecord" class="btn btn-secondary">清空</a>
        </form>
    </div>

    <!-- 罚款记录表格 -->
    <%
        List<Payrec> payrecList = (List<Payrec>) request.getAttribute("payrecList");
        if (payrecList == null || payrecList.isEmpty()) {
    %>
    <div class="empty-message">
        <p>暂无符合条件的罚款记录</p>
        <p>共 <%=totalCount != null ? totalCount : 0%> 条记录，当前页无数据。</p>
    </div>
    <%
    } else {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    %>
    <table class="fine-table">
        <thead>
        <tr>
            <th>流水号</th>
            <th>学号</th>
            <th>罚款金额</th>
            <th>罚款日期</th>
        </tr>
        </thead>
        <tbody>
        <%
            for (Payrec payrec : payrecList) {
        %>
        <tr>
            <td><%=payrec.getSerNum()%></td>
            <td><%=payrec.getSno()%></td>
            <td>¥<%=df.format(payrec.getPayAmount())%></td>
            <td><%=payrec.getPayDate() != null ? sdf.format(payrec.getPayDate()) : ""%></td>
        </tr>
        <%
            }
        %>
        </tbody>
    </table>
    <%
        }
    %>

    <!-- 分页 -->
    <%
        Integer currentPageNum = (Integer) request.getAttribute("pageNum");
        Integer pageSize = (Integer) request.getAttribute("pageSize");
        Integer totalPages = (Integer) request.getAttribute("totalPages");
        String keyword = (String) request.getAttribute("keyword");
        String startDate = (String) request.getAttribute("startDate");
        String endDate = (String) request.getAttribute("endDate");

        if (totalPages != null && totalPages > 1) {
            StringBuilder baseUrlBuilder = new StringBuilder(request.getContextPath() + "/admin/payrec?method=fineRecord");

            if (keyword != null && !keyword.trim().isEmpty()) {
                baseUrlBuilder.append("&keyword=").append(keyword);
            }
            if (startDate != null && !startDate.trim().isEmpty()) {
                baseUrlBuilder.append("&startDate=").append(startDate);
            }
            if (endDate != null && !endDate.trim().isEmpty()) {
                baseUrlBuilder.append("&endDate=").append(endDate);
            }

            String baseUrl = baseUrlBuilder.toString();
    %>
    <div class="pagination">
        <%
            // 上一页
            if (currentPageNum > 1) {
        %>
        <a href="<%=baseUrl%>&pageNum=<%=currentPageNum - 1%>&pageSize=<%=pageSize%>">&laquo; 上一页</a>
        <%
        } else {
        %>
        <span class="disabled">&laquo; 上一页</span>
        <%
            }

            // 页码
            int startPage = Math.max(1, currentPageNum - 2);
            int endPage = Math.min(totalPages, currentPageNum + 2);

            for (int i = startPage; i <= endPage; i++) {
                if (i == currentPageNum) {
        %>
        <a href="#" class="active"><%=i%></a>
        <%
        } else {
        %>
        <a href="<%=baseUrl%>&pageNum=<%=i%>&pageSize=<%=pageSize%>"><%=i%></a>
        <%
                }
            }

            // 下一页
            if (currentPageNum < totalPages) {
        %>
        <a href="<%=baseUrl%>&pageNum=<%=currentPageNum + 1%>&pageSize=<%=pageSize%>">下一页 &raquo;</a>
        <%
        } else {
        %>
        <span class="disabled">下一页 &raquo;</span>
        <%
            }
        %>
    </div>
    <%
        }
    %>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 表单提交前验证
        document.querySelector('.search-form').addEventListener('submit', function(e) {
            const startDate = document.getElementById('startDate').value;
            const endDate = document.getElementById('endDate').value;

            if (startDate && endDate && new Date(startDate) > new Date(endDate)) {
                alert('开始日期不能晚于结束日期！');
                e.preventDefault();
                return false;
            }
        });
    });
</script>
</body>
</html>